﻿using System; 
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic; 
using System.Data;
using RBS.DBUtility;
using RBS.Common;
namespace RBS.Core.DAL  
	.Hotel
{
	 	//酒店座位信息表
		public partial class H_Hotel_Seat
	{
   		     
		public bool Exists(int Id)
		{

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from H_Hotel_Seat");
            strSql.Append(" where ");
            strSql.Append(" Id = @Id  ");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
		}
		
				
		
		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(RBS.Core.Model.Hotel.H_Hotel_Seat model)
		{
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into H_Hotel_Seat(");
            strSql.Append("AOBT,Status,HotelId,Floor,Category,Hall,RoomNumber,Name,Numbers,Picture");
            strSql.Append(") values (");
            strSql.Append("@AOBT,@Status,@HotelId,@Floor,@Category,@Hall,@RoomNumber,@Name,@Numbers,@Picture");
            strSql.Append(") ");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
			            new SqlParameter("@AOBT", SqlDbType.TinyInt,1) ,            
                        new SqlParameter("@Status", SqlDbType.Int,4) ,            
                        new SqlParameter("@HotelId", SqlDbType.Int,4) ,            
                        new SqlParameter("@Floor", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@Category", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@Hall", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@RoomNumber", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@Name", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@Numbers", SqlDbType.Int,4) ,            
                        new SqlParameter("@Picture", SqlDbType.NVarChar,500)             
              
            };

            parameters[0].Value = model.AOBT;
            parameters[1].Value = model.Status;
            parameters[2].Value = model.HotelId;
            parameters[3].Value = model.Floor;
            parameters[4].Value = model.Category;
            parameters[5].Value = model.Hall;
            parameters[6].Value = model.RoomNumber;
            parameters[7].Value = model.Name;
            parameters[8].Value = model.Numbers;
            parameters[9].Value = model.Picture;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {

                return Convert.ToInt32(obj);

            }			   
            			
		}
		
		
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(RBS.Core.Model.Hotel.H_Hotel_Seat model)
		{
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update H_Hotel_Seat set ");

            strSql.Append(" AOBT = @AOBT , ");
            strSql.Append(" Status = @Status , ");
            strSql.Append(" HotelId = @HotelId , ");
            strSql.Append(" Floor = @Floor , ");
            strSql.Append(" Category = @Category , ");
            strSql.Append(" Hall = @Hall , ");
            strSql.Append(" RoomNumber = @RoomNumber , ");
            strSql.Append(" Name = @Name , ");
            strSql.Append(" Numbers = @Numbers , ");
            strSql.Append(" Picture = @Picture  ");
            strSql.Append(" where Id=@Id ");

            SqlParameter[] parameters = {
			            new SqlParameter("@Id", SqlDbType.Int,4) ,            
                        new SqlParameter("@AOBT", SqlDbType.TinyInt,1) ,            
                        new SqlParameter("@Status", SqlDbType.Int,4) ,            
                        new SqlParameter("@HotelId", SqlDbType.Int,4) ,            
                        new SqlParameter("@Floor", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@Category", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@Hall", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@RoomNumber", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@Name", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@Numbers", SqlDbType.Int,4) ,            
                        new SqlParameter("@Picture", SqlDbType.NVarChar,500)             
              
            };

            parameters[0].Value = model.Id;
            parameters[1].Value = model.AOBT;
            parameters[2].Value = model.Status;
            parameters[3].Value = model.HotelId;
            parameters[4].Value = model.Floor;
            parameters[5].Value = model.Category;
            parameters[6].Value = model.Hall;
            parameters[7].Value = model.RoomNumber;
            parameters[8].Value = model.Name;
            parameters[9].Value = model.Numbers;
            parameters[10].Value = model.Picture;
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
		}
		
		
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int Id)
		{

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from H_Hotel_Seat ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;


            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
		}
		
				/// <summary>
		/// 批量删除一批数据
		/// </summary>
		public bool DeleteList(string Idlist )
		{
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from H_Hotel_Seat ");
            strSql.Append(" where ID in (" + Idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
		}
				
		
		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public RBS.Core.Model.Hotel.H_Hotel_Seat GetModel(int Id)
		{

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id, AOBT, Status, HotelId, Floor, Category, Hall, RoomNumber, Name, Numbers, Picture  ");
            strSql.Append("  from H_Hotel_Seat ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;


            RBS.Core.Model.Hotel.H_Hotel_Seat model = new RBS.Core.Model.Hotel.H_Hotel_Seat();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);

            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["AOBT"].ToString() != "")
                {
                    model.AOBT = int.Parse(ds.Tables[0].Rows[0]["AOBT"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Status"].ToString() != "")
                {
                    model.Status = int.Parse(ds.Tables[0].Rows[0]["Status"].ToString());
                }
                if (ds.Tables[0].Rows[0]["HotelId"].ToString() != "")
                {
                    model.HotelId = int.Parse(ds.Tables[0].Rows[0]["HotelId"].ToString());
                }
                model.Floor = ds.Tables[0].Rows[0]["Floor"].ToString();
                model.Category = ds.Tables[0].Rows[0]["Category"].ToString();
                model.Hall = ds.Tables[0].Rows[0]["Hall"].ToString();
                model.RoomNumber = ds.Tables[0].Rows[0]["RoomNumber"].ToString();
                model.Name = ds.Tables[0].Rows[0]["Name"].ToString();
                if (ds.Tables[0].Rows[0]["Numbers"].ToString() != "")
                {
                    model.Numbers = int.Parse(ds.Tables[0].Rows[0]["Numbers"].ToString());
                }
                model.Picture = ds.Tables[0].Rows[0]["Picture"].ToString();

                return model;
            }
            else
            {
                return null;
            }
		}
		
		
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select * ");
			strSql.Append(" FROM H_Hotel_Seat ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}
		
		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" * ");
			strSql.Append(" FROM H_Hotel_Seat ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}
		
		
		 #region  ExtensionMethod
        /// <summary>
        /// 获得查询分页数据
        /// </summary>
        public DataSet GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * FROM H_Hotel_Seat ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where 1=1 " + strWhere);
            }
            recordCount = Convert.ToInt32(DbHelperSQL.GetSingle(PagingHelper.CreateCountingSql(strSql.ToString())));
            return DbHelperSQL.Query(PagingHelper.CreatePagingSql(recordCount, pageSize, pageIndex, strSql.ToString(), filedOrder));
        }
		#endregion  ExtensionMethod

		    public bool UpdateStatus(int hotelId, string dineTableId,int status)
		    {
                var strSql = "update H_Hotel_Seat  set  Status=@status where  HotelId=@hotelId and Id=@dineTableId";
                SqlParameter[] parameters = {
			            new SqlParameter("@hotelId", SqlDbType.Int,4) ,   
                        new SqlParameter("@dineTableId", SqlDbType.Int,4),
                        new SqlParameter("@status", SqlDbType.Int,4),
                 }; 
                parameters[0].Value = hotelId;
                parameters[1].Value = dineTableId;
                parameters[2].Value = status; 
                int rows = DbHelperSQL.ExecuteSql(strSql, parameters);
                if (rows > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
		    }

            /// <summary>
            /// 获取能订餐的座位
            /// </summary>
            /// <param name="hid"></param>
            /// <param name="time1"></param>
            /// <param name="time2"></param>
            /// <returns></returns>
            public DataSet GetSeatList(int hid, DateTime time1, DateTime time2)
            {
                const string strsql = @" with 
	                            Orders as
	                              (
		                            select H_Id as hid,DineTable as seatid, COUNT(1) as islook from D_Order where H_Id=@hid and OrderTime between @time1 and @time2  and OrderState  in (2,4)  group by H_Id,DineTable
	                              )
                            select s.* from H_Hotel_Seat s left join Orders o
                             on s.Id =o.seatid where HotelId=@hid  and o.islook is null and Status=0";

                SqlParameter[] parameters = {
					new SqlParameter("@hid", SqlDbType.Int,4),
                    new SqlParameter("@time1", SqlDbType.DateTime,8),
                    new SqlParameter("@time2", SqlDbType.DateTime,8)
			};
                parameters[0].Value = hid;
                parameters[1].Value = time1;
                parameters[2].Value = time2;
                return DbHelperSQL.Query(strsql, parameters);

            }
	}
}

